package com.vernhe.netty;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;

/**
 * 实现客户端发送一个请求，服务端返回Hello Netty
 */
public class Hello {
    public static void main(String[] args) throws InterruptedException {
        //定义一组线程组
        //主线程组，接收连接，不做实际的事
        EventLoopGroup boosGroup = new NioEventLoopGroup();
        //从线程组，做相应的处理
        EventLoopGroup workerGroup = new NioEventLoopGroup();

        try {
            //netty服务端启动类
            ServerBootstrap serverBootstrap = new ServerBootstrap();
            serverBootstrap.group(boosGroup,workerGroup)            //设置主从线程组
                           .channel(NioServerSocketChannel.class)   //设置nio双向通道
                           .childHandler(new HelloServerInitializer());                     //子处理器，用于处理workerGroup
            //启动server,并设置8088端口号，同时启动方式为同步方式
            ChannelFuture channelFuture = serverBootstrap.bind(8088).sync();  //等待8088端口

            //关闭channel，设置同步方式
            channelFuture.channel().closeFuture().sync();
        } finally {
            boosGroup.shutdownGracefully();
            workerGroup.shutdownGracefully();
        }
    }
}
